package com.maxlen.common.utils;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 赋值工具类
 */
@SuppressWarnings("unused")
public class CopyUtils {

    public static <T> List<T> copyList(List<?> list, Class<T> clazz) {
        List<T> result = new ArrayList<>(list.size());
        for (Object source : list) {
            T target;
            try {
                target = clazz.getDeclaredConstructor().newInstance();
            } catch (Exception e) {
                throw new RuntimeException();
            }
            BeanUtils.copyProperties(source, target);
            result.add(target);
        }
        return result;
    }

    public static <T> T copy(Object source, Class<T> clazz) {
        T target;
        try {
            target = clazz.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException();
        }
        BeanUtils.copyProperties(source, target);
        return target;
    }

}
